package com.jinsi.controller;

import com.jinsi.pojo.Stu;
import com.jinsi.pojo.Tea;
import com.jinsi.service.StuSerivce;
import com.jinsi.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class ExcelController {

    @Autowired
    private StuSerivce stuSerivce;
    /**
     * 导出用户列表
     *
     * @param response response
     * @throws IOException
     */
    @GetMapping("export")
    public void exportUserList(HttpServletResponse response) throws IOException {
        ArrayList<Tea> users = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            Tea user = new Tea();
            user.setId(i);
            user.setName("暴走萝莉" + i);
            user.setBirthday(new Date());
            user.setSex(2);
            user.setPassingRate(i);
            users.add(user);
        }

        String today = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        ExcelUtils.exportExcel(users, "用户信息列表 " + today, "用户信息列表", Tea.class, "用户信息列表 " + today, response);
    }

    /**
     * 导入用户信息
     *
     * @param excelFile
     * @throws IOException
     */
    @GetMapping("import")
    public void importUser(MultipartFile excelFile) throws IOException {

        List<Stu> users = ExcelUtils.importExcel(excelFile, Stu.class);
    }

}
